<?php
class Place_Model_MerchantPlace extends Zend_Db_Table{
    protected $_name  = "merchant_place";
    protected $_primary = "place_id";
    
    public function init()
    {
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        $this->_db = $DB;
        parent::_setupDatabaseAdapter();
    }
    
    public function getNextPlaceId($currentPlaceId){
        $registry   = Zend_Registry::getInstance();
        $constant = $registry->constants;
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        
        $requestStatus = $constant->APPROVE_REQUEST;
        
        $selectNextPlaceId = $DB->select()
                                ->from(array('p'=>'merchant_place'),
                                         array('MIN(p.place_id) min_place_id'))
                                ->where('status = ?', $requestStatus)
                                ->where('p.place_id > ?', $currentPlaceId);
        $nextPlaceIdResult = $DB->fetchAll($selectNextPlaceId);
        $nextPlaceId = $nextPlaceIdResult[0]->min_place_id;
        if($nextPlaceId == '' || $nextPlaceId == 0){
            return $currentPlaceId;
        }
        return $nextPlaceId;
        
    }
    
    public function getPlaceByStatus($approve, $page=1){
        $registry   = Zend_Registry::getInstance();
        $constant = $registry->constants;
        $PAGING_NUMBER = $constant->PAGING_NUMBER;
        $start = ($page - 1)*$PAGING_NUMBER;
        
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        
        $select = $DB   ->select()
                        ->from(array('p'=>'merchant_place'),
                                 array('p.place_id','p.name', 'p.address',
                                       'p.phone', 'p.mail', 
                                       'p.website' ))
                        ->where('status = ?', $approve)
                        ->order('name')
                        ->limit($PAGING_NUMBER, $start);
        $result = $DB->fetchAll($select);
        return $result;
    }
    
    public function getPlaceNotApproved($userId,$page=1){
        $registry   = Zend_Registry::getInstance();
        $constant = $registry->constants;
        $PAGING_NUMBER = $constant->PAGING_NUMBER;
        $start = ($page - 1)*$PAGING_NUMBER;
        $approved = $constant->APPROVED;
        
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        
        $statusNotApproved = array($constant->APPROVE_NOT, 
                                    $constant->APPROVE_REQUEST,
                                    $constant->REJECT_APPROVE);
        
        $select = $DB   ->select()
                        ->from(array('p'=>'merchant_place'),
                                 array('p.place_id','p.name', 'p.address',
                                       'p.phone', 'p.mail', 
                                       'p.website', 'p.status' ))
                        ->where('sale_user_id = ?', $userId)
                        ->where('status IN (?)', $statusNotApproved)
                        ->order('name')
                        ->limit($PAGING_NUMBER, $start);
        $result = $DB->fetchAll($select);
        return $result;
    }
    
    public function getPlaceNotApprovedNumber($userId){
        $registry   = Zend_Registry::getInstance();
        $constant = $registry->constants;
        $approved = $constant->APPROVED;
        
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        
        $statusNotApproved = array($constant->APPROVE_NOT, 
                                    $constant->APPROVE_REQUEST,
                                    $constant->REJECT_APPROVE);
        
        $select = $DB   ->select()
                        ->from(array('p'=>'merchant_place'),
                                array('place_number'=>'count(place_id)'))
                        ->where('sale_user_id = ?', $userId)
                        ->where('status IN (?)', $statusNotApproved);
        $result = $DB->fetchAll($select);
        $placeNumber = $result[0]->place_number;
        return $placeNumber;
    }
    
    public function getPlaceNumberByStatus($approve){
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        
        $select = $DB   ->select()
                        ->from(array('p'=>'merchant_place'),
                                array('place_number'=>'count(place_id)'))
                        ->where('status = ?', $approve);
        $result = $DB->fetchAll($select);
        $placeNumber = $result[0]->place_number;
        return $placeNumber;
    }
    
    public function getPlaceOfTeamByStatus($teamId, $status, $page=1){
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        
        $registry   = Zend_Registry::getInstance();
        $constant = $registry->constants;
        $PAGING_NUMBER = $constant->PAGING_NUMBER;
        $startNumber = ($page - 1)*$PAGING_NUMBER;
        
        $placeListResult = array();
        
        $getPlace = "SELECT p.*".
                      " FROM merchant_place p, sale_user_team t ".
                      " WHERE p.sale_user_id = t.sale_user_id".
                      " AND p.`status` = ".$status.
                      " AND t.sale_team_id = ".$teamId.
                      " ORDER BY p.place_id".
                      " LIMIT ".$startNumber.", ".$PAGING_NUMBER;
        $placeListResult = $DB->fetchAll($getPlace);
        
        return $placeListResult;
    }
    
    public function getPlaceNumberOfTeam($teamId, $status){
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        
        $registry   = Zend_Registry::getInstance();
        $constant = $registry->constants;
        
        $placeListResult = array();
        
        $getPlace = "SELECT count(p.place_id) number".
                      " FROM merchant_place p, sale_user_team t ".
                      " WHERE p.sale_user_id = t.sale_user_id".
                      " AND p.`status` = ".$status.
                      " AND t.sale_team_id = ".$teamId;
        $placeListResult = $DB->fetchAll($getPlace);
        
        return $placeListResult[0]->number;
    }
    
    
    public function searchMerchantPlace($name, $address, 
                                        $phone, $website,
                                        $approve ,$page=1){
        $registry   = Zend_Registry::getInstance();
        $constant = $registry->constants;
        $PAGING_NUMBER = $constant->PAGING_NUMBER;
        $start = ($page - 1)*$PAGING_NUMBER;
        
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        
        $merchantNameQuotes = $name;
        $merchantAddressQuotes = $address;
        if(strlen(trim($name)) > 0){
            $merchantNameQuotes = '"'.$name.'"';    
        }
        if(strlen(trim($address)) > 0){
            $merchantAddressQuotes  = '"'.$address.'"'; 
        }
        $merchantPhone = $phone;
        $merchantEmail = $website;
        
        $searchQuery = '';
        if($merchantEmail != ''){
            if($merchantPhone != ''){
                $searchQuery = $DB  ->select()
                            ->from('merchant_place')
                            ->where("MATCH(name) AGAINST (? IN BOOLEAN MODE) OR ? = ''", $merchantNameQuotes)
                            ->where("MATCH(address) AGAINST (? IN BOOLEAN MODE) OR ? = ''", $merchantAddressQuotes)
                            ->where("phone LIKE ?", "%".$merchantPhone."%")
                            ->where("website LIKE ?", "%".$merchantEmail."%")
                            ->where('status = ?', $approve)
                            ->order('name')
                            ->limit($PAGING_NUMBER, $start);
            }else{
                $searchQuery = $DB  ->select()
                            ->from('merchant_place')
                            ->where("MATCH(name) AGAINST (? IN BOOLEAN MODE) OR ? = ''", $merchantNameQuotes)
                            ->where("MATCH(address) AGAINST (? IN BOOLEAN MODE) OR ? = '' ", $merchantAddressQuotes)
                            ->where("website LIKE ?", "%".$merchantEmail."%")
                            ->where('status = ?', $approve)
                            ->order('name')
                            ->limit($PAGING_NUMBER, $start);
            }
        }else{
            if($merchantPhone != ''){
                $searchQuery = $DB  ->select()
                            ->from('merchant_place')
                            ->where("MATCH(name) AGAINST (? IN BOOLEAN MODE) OR ? = ''", $merchantNameQuotes)
                            ->where("MATCH(address) AGAINST (? IN BOOLEAN MODE) OR ? = '' ", $merchantAddressQuotes)
                            ->where("phone LIKE ?", "%".$merchantPhone."%")
                            ->where('status = ?', $approve)
                            ->order('name')
                            ->limit($PAGING_NUMBER, $start);
            }else{
                $searchQuery = $DB  ->select()
                            ->from('merchant_place')
                            ->where("MATCH(name) AGAINST (? IN BOOLEAN MODE) OR ? = ''", $merchantNameQuotes)
                            ->where("MATCH(address) AGAINST (? IN BOOLEAN MODE) OR ? = '' ", $merchantAddressQuotes)
                            ->where('status = ?', $approve)
                            ->order('name')
                            ->limit($PAGING_NUMBER, $start);
            }
        }
        $searchResult = $DB->fetchAll($searchQuery);
        return $searchResult;
    }


    public function getSearchPlaceNumber($name, $address, 
                                        $phone, $website,
                                        $approve){
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        
        $merchantNameQuotes = $name;
        $merchantAddressQuotes = $address;
        if(strlen(trim($name)) > 0){
            $merchantNameQuotes = '"'.$name.'"';    
        }
        if(strlen(trim($address)) > 0){
            $merchantAddressQuotes  = '"'.$address.'"'; 
        }
        
        $merchantPhone = $phone;
        $merchantEmail = $website;
        
        $searchCount = '';
        if($merchantEmail != ''){
            if($merchantPhone != ''){
                $searchCount = $DB  ->select()
                            ->from(array('p' => 'merchant_place'),
                                    array('search_number' => 'count(place_id)'))
                            ->where("MATCH(name) AGAINST (? IN BOOLEAN MODE) OR ? = ''", $merchantNameQuotes)
                            ->where("MATCH(address) AGAINST (? IN BOOLEAN MODE) OR ? = ''", $merchantAddressQuotes)
                            ->where("phone LIKE ?", "%".$merchantPhone."%")
                            ->where("website LIKE ?", "%".$merchantEmail."%")
                            ->where('status = ?', $approve);
            }else{
                $searchCount = $DB  ->select()
                            ->from(array('p' => 'merchant_place'),
                                    array('search_number' => 'count(place_id)'))
                            ->where("MATCH(name) AGAINST (? IN BOOLEAN MODE) OR ? = ''", $merchantNameQuotes)
                            ->where("MATCH(address) AGAINST (? IN BOOLEAN MODE) OR ? = '' ", $merchantAddressQuotes)
                            ->where("website LIKE ?", "%".$merchantEmail."%")
                            ->where('status = ?', $approve);
            }
        }else{
            if($merchantPhone != ''){
                $searchCount = $DB  ->select()
                            ->from(array('p' => 'merchant_place'),
                                    array('search_number' => 'count(place_id)'))
                            ->where("MATCH(name) AGAINST (? IN BOOLEAN MODE) OR ? = ''", $merchantNameQuotes)
                            ->where("MATCH(address) AGAINST (? IN BOOLEAN MODE) OR ? = '' ", $merchantAddressQuotes)
                            ->where("phone LIKE ?", "%".$merchantPhone."%")
                            ->where('status = ?', $approve);
            }else{
                $searchCount = $DB  ->select()
                            ->from(array('p' => 'merchant_place'),
                                    array('search_number' => 'count(place_id)'))
                            ->where("MATCH(name) AGAINST (? IN BOOLEAN MODE) OR ? = ''", $merchantNameQuotes)
                            ->where("MATCH(address) AGAINST (? IN BOOLEAN MODE) OR ? = '' ", $merchantAddressQuotes)
                            ->where('status = ?', $approve);
            }
        }
        $countSearchResult = $DB->fetchAll($searchCount);
        $result = $countSearchResult[0]->search_number;
        return $result; 
    }
    
    public function getPlaceDetail($placeId){
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        $select = $DB   ->select()
                        ->from('merchant_place')
                        ->where('place_id = ?',$placeId);
        $merchantDealResult = $DB->fetchAll($select);
        $result = $merchantDealResult[0];
        return $result;
    }
    
    public function save($arrParam){
        $row =  $this->fetchNew();
        foreach ($arrParam as $key => $value) {
            $row->$key = $value;
        }
        return $row->save();
    }
    
    public function edit($arrParam){
        $where = 'place_id = ' . $arrParam['place_id'];
        $row =  $this->fetchRow($where);
        foreach ($arrParam as $key => $value) {
            $row->$key = $value;
        }
        $row->save();
    }
    
    
}
